﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FileAuth.aspx.cs" Inherits="Web.FileAuth"%>

<!doctype html>
<html lang="en">
<head>
	<title>文件授权</title>
	<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
    <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
    <META HTTP-EQUIV="Expires" CONTENT="0">
    <link href="yaml/core/base.min.css" rel="stylesheet" />
    <link href="yaml/forms/gray-theme.css" rel="stylesheet" />
    <link href="yaml/screen/typography.css" rel="stylesheet" />
    <!--[if lte IE 7]>
      <link rel="stylesheet" href="yaml/core/iehacks.min.css" type="text/css"/>
      <![endif]-->
    <!--[if lt IE 9]>
      <script src="yaml/lib/html5shiv/html5shiv.js"></script>
      <![endif]-->
    <script src="Scripts/jquery-1.7.min.js" type="text/javascript"></script>
	<style>
		table DIV { BORDER: #88b6d2 1px solid;width:130px;text-align:center;overflow:auto;}
		.tdDel,.lblDel{cursor:pointer;color:#ff4f17;}
		.tdDel:hover,.lblDel:hover{color:#fff;background:#ff4f17;}
        td{ line-height:12px;height: 12px;}
        .fRight{ float: right;}

        .ym-form label{ display: inline;}
	</style>
</HEAD>
<body>
<form id="Form1" runat="server" class="ym-form">
	<table>
    <thead>
		<tr>
			<td colspan="3">
				<label for="Dept">部门:</label><select class="ym-inline" id="Dept" runat="server" onchange="deptRoleChange();"></select>
				<label for="Position">职位:</label><select class="ym-inline" id="Position" runat="server" onchange="deptRoleChange();"></select>
			</td>
		</tr>
		<tr>
			<td rowspan="2">
				<asp:Repeater ID="rptEmployeeList" EnableViewState="False" Runat="server">
					<HeaderTemplate>
						<span>可选人员</span><input id="chkEmpChooseLeft" type="checkbox" onclick="chooseAll(this);" /><label class="lblDel" for="chkEmpChooseLeft">全选</label>
						<div style="height:422px;">
							<table class="list" id="tbEmployeeList">
					</HeaderTemplate>
					<ItemTemplate>
						<tr>
							<td><input type="checkbox" value='<%# DataBinder.Eval(Container.DataItem,"Id") %>'></td>
							<td><%# DataBinder.Eval(Container.DataItem, "Name")%></td>
						</tr>
					</ItemTemplate>
					<FooterTemplate>
						</table></div>
					</FooterTemplate>
				</asp:Repeater>
			</td>
			<td style="vertical-align:middle;">
				<input id="btnEmployeeAdd" class="btn" type="button" value="==>" onclick="addRemove();" />
			</td>
			<td>
				<asp:Repeater ID="rptChoosedEmployeeList" EnableViewState="False" Runat="server">
					<HeaderTemplate>
						<span>主送人员</span><label class="lblDel" onclick="removeAll('tbChoosedEmployeeList','tbEmployeeList');">[全删]</label>
						<div style="height:180px;"><table class="choosedList" id="tbChoosedEmployeeList">
					</HeaderTemplate>
					<ItemTemplate>
						<tr>
							<td><input type="hidden" value='<%# DataBinder.Eval(Container.DataItem,"Id") %>'><%# DataBinder.Eval(Container.DataItem, "Name")%></td>
							<td><span class="tdDel" title="移除" onclick="remove(this,'tbEmployeeList');">&nbsp;X&nbsp;</span></td>
						</tr>
					</ItemTemplate>
					<FooterTemplate>
						</table></div>
					</FooterTemplate>
				</asp:Repeater>
			</td>
		</tr>
		<tr>
			<td style="vertical-align:middle;">
				<input id="btnEmployeeAdd2" class="btn" type="button" value="==>" onclick="addRemove();" />
			</td>
			<td>
				<asp:Repeater ID="rptChoosedEmployeeList2" EnableViewState="False" Runat="server">
					<HeaderTemplate>
						<span>抄送人员</span><label class="lblDel" onclick="removeAll('tbChoosedEmployeeList2','tbEmployeeList');">[全删]</label>
						<div style="height:180px;"><table class="choosedList" id="tbChoosedEmployeeList2">
					</HeaderTemplate>
					<ItemTemplate>
						<tr>
							<td><input type="hidden" value='<%# DataBinder.Eval(Container.DataItem,"Id") %>'><%# DataBinder.Eval(Container.DataItem, "Name")%></td>
							<td><span class="tdDel" title="移除" onclick="remove(this,'tbEmployeeList');">&nbsp;X&nbsp;</span></td>
						</tr>
					</ItemTemplate>
					<FooterTemplate>
						</table></div>
					</FooterTemplate>
				</asp:Repeater>
			</td>
		</tr>
    </thead>
	</table>
	<div class="fRight">
		<input id="btnSubmit" class="btn" type="button" value="保存" onclick="authSubmit();"/>
		<input id="btnClose" class="btn" type="button" value="关闭" onclick="window.close();"/>
	</div>
</form>
</body>
<script language="javascript">
	//使用数组取得表格值   
	function getRowText(objId) {
	    var arr = new Array();
	    var count = 0;
	    var tb = document.getElementById(objId);
	    for (var i = 0; i < tb.rows.length; i++) {
	        var row = tb.rows[i];
	        arr[count] = row.cells[0].firstChild.value;
	        count++;
	    }
	    return arr;
	}
	//授权提交
	function authSubmit() {
	    if (!confirm("确认提交吗?")) {
	        return false;
	    }
	    var arr = getRowText("tbChoosedEmployeeList");
	    var empIds = arr.join(",");
	    arr = getRowText("tbChoosedEmployeeList2");
	    var empIds2 = arr.join(",");
	    var fileId = getSpecProp("fileId");
	    $.ajax({
	        type: "POST",
	        url: "File.mvc/AuthOpt",
	        data: "fileId=" + fileId + "&empIds=" + empIds + "&empIds2=" + empIds2,
	        dataType: "json",
	        success: function (response) {
	            if (response== "Succeed") {
	                alert("权限提交成功!");
	                window.close();
	            }
	            else {
	                alert(response);
	            }
	        }
	    });
	}
	//取网址中的特定搜索字串值
	function getSpecProp(prop) {
	    var searchArr = location.search.substr(1).split("&");
	    for (var i = 0; i < searchArr.length; i++) {
	        if (searchArr[i].split("=")[0] == prop) {
	            return searchArr[i].split("=")[1];
	        }
	    }
	    return "";
	}
	//增加/删除 授权 人员/角色
	function addRemove() {
	    var tbFrom;
	    var tbTo;
	    var objID = event.srcElement.id;
	    if (objID == "btnEmployeeAdd") {
	        tbFrom = tbEmployeeList;
	        tbTo = tbChoosedEmployeeList;
	    }
	    else if (objID == "btnEmployeeAdd2") {
	        tbFrom = tbEmployeeList;
	        tbTo = tbChoosedEmployeeList2;
	    }
	    else if (objID == "btnRoleAdd") {
	        tbFrom = tbRoleList;
	        tbTo = tbChoosedRoleList;
	    }
	    var arr = new Array();
	    for (var i = 0; i < tbFrom.rows.length; ) {
	        var row = tbFrom.rows[i];
	        //取得CHECKBOX   
	        var objCheck = row.cells[0].getElementsByTagName("INPUT")[0];
	        if (objCheck != null && objCheck.checked) {
	            //tbChoosedEmployeeList.appendChild(row);
	            var newRow = tbTo.insertRow(0);
	            var cell = newRow.insertCell(0);
	            cell.innerHTML = '<input type="hidden" value="' + objCheck.value + '">' + row.cells[1].innerHTML;
	            cell = newRow.insertCell(1);
	            cell.innerHTML = '<span class="tdDel" title="移除" onclick="remove(this,\'' + tbFrom.id + '\');">&nbsp;X&nbsp;</span>';
	            row.parentNode.removeChild(row);
	        }
	        else {
	            i++;
	        }
	    }
	}
	//实现全选/全空
	function chooseAll(obj) {
	    var tb;
	    if (obj.id == "chkEmpChooseLeft") {
	        tb = tbEmployeeList;
	    }
	    else if (obj.id == "chkRoleChooseLeft") {
	        tb = tbRoleList;
	    }
	    if (obj.checked) {
	        for (var i = 0; i < tb.rows.length; i++) {
	            var row = tb.rows[i];
	            var objCheck = row.cells[0].getElementsByTagName("INPUT")[0];
	            objCheck.checked = true;
	        }
	    }
	    else {
	        for (var i = 0; i < tb.rows.length; i++) {
	            var row = tb.rows[i];
	            var objCheck = row.cells[0].getElementsByTagName("INPUT")[0];
	            objCheck.checked = false;
	        }
	    }
	}
	function deptRoleChange() {
	    $.ajax({
	        type: "POST",
	        url: "File.mvc/GetUnauthedEmp",
	        data: "deptId=" + $("#Dept").val() + "&posId=" + $("#Position").val(),
	        dataType: "json",
	        success: function (response) {
	            if (response.error != null) {
	                alert(response.error.Message);
	            }
	            else {
	                while (tbEmployeeList.rows.length > 0) {
	                    tbEmployeeList.deleteRow();
	                }
	                var choosedEmployeeArr = getRowText("tbChoosedEmployeeList");
	                var choosedEmployeeArr2 = getRowText("tbChoosedEmployeeList2");
	                var newArr = choosedEmployeeArr.concat(choosedEmployeeArr2);
	                var choosedEmployeeIds = ',' + newArr.join(",") + ',';
	                for (var i = 0; i < response.length; i++) {
	                    var searchId = ',' + response[i].Id + ',';
	                    if (choosedEmployeeIds.indexOf(searchId) < 0) {
	                        var newRow = tbEmployeeList.insertRow(tbEmployeeList.rows.length);
	                        var cell = newRow.insertCell(0);
	                        cell.innerHTML = '<input type="checkbox" value=\'' + response[i].Id + '\'>';
	                        cell = newRow.insertCell(1);
	                        cell.innerHTML = response[i].Name;
	                    }
	                }
	            }
	        }
	    });
	}

	function remove(obj, tbID) {
	    var tb = document.getElementById(tbID);
	    var newRow = tb.insertRow(0);
	    var cell = newRow.insertCell(0);
	    cell.innerHTML = '<input type="checkbox" value=\'' + obj.parentElement.parentElement.cells[0].firstChild.value + '\' checked>';
	    cell = newRow.insertCell(1);
	    cell.innerHTML = obj.parentElement.parentElement.cells[0].innerText;
	    obj.parentElement.parentElement.parentElement.removeChild(obj.parentElement.parentElement); //移除该行
	}
	function removeAll(fromTbId, toTbId) {
	    var fromTb = document.getElementById(fromTbId);
	    var toTb = document.getElementById(toTbId);
	    while (fromTb.rows.length > 0) {
	        var row = fromTb.rows[0];
	        var newRow = toTb.insertRow(0);
	        var cell = newRow.insertCell(0);
	        cell.innerHTML = '<input type="checkbox" value=\'' + row.cells[0].firstChild.value + '\' checked>';
	        cell = newRow.insertCell(1);
	        cell.innerHTML = row.cells[0].innerText;
	        fromTb.deleteRow(0); //移除该行
	    }
	}
</script>
</HTML>